package com.springboot.control;

import org.apache.http.HttpRequest;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;

/**
 * Created by 41579 on 2017/3/14.
 */
@Controller
@RequestMapping("/test")
public class TestControl {
    @Autowired
    private Test test;

    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public String test(String code) throws IOException {
        CloseableHttpClient httpclient = HttpClients.createDefault();

        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=wx378636cb6198f8d7&secret=235b1ac962df5d173c7e2156024e987c&js_code="+code+"&grant_type=authorization_code";
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = httpclient.execute(httpget);
        String result = EntityUtils.toString(response.getEntity());
        System.out.println("result = [" + result + "]");
        return "test2";
    }
}
